<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/21 0021
 * Time: 17:21
 */

namespace app\controllers;


use app\common\base\BaseController;
use app\models\TbArticle;
use app\models\UploadForm;
use Yii;
use yii\web\UploadedFile;

class ArticleController extends BaseController
{

    /**
     * 调整文章添加页
     * @return string
     */
    public function actionPublisHarticle(){
        return $this->render('publisharticle');
    }

    public function actionAddPublisHarticle(){
        $title = $this->getParam("question_content");//获取文章标题
        $content = $this->getParam("question_detail");//获取文章内容
        $username = Yii::$app->session->get("regname");//文章作者
        date_default_timezone_set('PRC');//设置中国时区
        $datetime = date('Y-m-d H:i:s',time());//获取当前系统时间
        $tbarticle = new TbArticle();
        $tbarticle->title = $title;
        $tbarticle->content = $content;
        $tbarticle->author = $username;
        $tbarticle->now = $datetime;
        if($title == null ){
            return $this->out(false,'文章添加失败,文章标题必须填写内容哦！');
        }else if ($content == null){
            return $this->out(false,'文章添加失败,文章内容必须填写内容哦！');
        }
        if ($tbarticle->save()){
            return $this->out(true,'文章添加成功');
        }
    }

    public function actionUpload(){
        $model =new UploadForm();

        if(Yii::$app->request->isPost){
            $model->file =UploadedFile::getInstance($model,'file');
            $name=$model->file->baseName .','.$model->file->extension;
            if ($model->file && $model->validate()){
                $model->file->saveAs('./image/'.$name);
            }
        }
        return $this->render('upload',['model'=>$model]);
    }
}